package org.jctc.els.modules.biz.service.pro.impl;


import cn.hutool.core.util.StrUtil;
import org.jctc.els.modules.biz.entity.pro.ProjectReviewDetailed;
import org.jctc.els.modules.biz.mapper.pro.ProjectReviewDetailedMapper;
import org.jctc.els.modules.biz.service.pro.ProjectReviewDetailedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
/**
 * 评审明细表;(els_project_review_detailed)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2022-6-21
 */
@Service
public class ProjectReviewDetailedServiceImpl implements ProjectReviewDetailedService {
    @Autowired
    private ProjectReviewDetailedMapper projectReviewDetailedMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public ProjectReviewDetailed queryById(Long id){
        return projectReviewDetailedMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param projectReviewDetailed 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<ProjectReviewDetailed> paginQuery(ProjectReviewDetailed projectReviewDetailed, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<ProjectReviewDetailed> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(projectReviewDetailed.getName())){
            queryWrapper.eq(ProjectReviewDetailed::getName, projectReviewDetailed.getName());
        }
        if(StrUtil.isNotBlank(projectReviewDetailed.getParentIdPath())){
            queryWrapper.eq(ProjectReviewDetailed::getParentIdPath, projectReviewDetailed.getParentIdPath());
        }
        if(StrUtil.isNotBlank(projectReviewDetailed.getOptionIds())){
            queryWrapper.eq(ProjectReviewDetailed::getOptionIds, projectReviewDetailed.getOptionIds());
        }
        if(StrUtil.isNotBlank(projectReviewDetailed.getRemark())){
            queryWrapper.eq(ProjectReviewDetailed::getRemark, projectReviewDetailed.getRemark());
        }
        //2. 执行分页查询
        Page<ProjectReviewDetailed> pagin = new Page<>(current , size , true);
        IPage<ProjectReviewDetailed> selectResult = projectReviewDetailedMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param projectReviewDetailed 实例对象
     * @return 实例对象
     */
    public ProjectReviewDetailed insert(ProjectReviewDetailed projectReviewDetailed){
        projectReviewDetailedMapper.insert(projectReviewDetailed);
        return projectReviewDetailed;
    }

    /**
     * 更新数据
     *
     * @param projectReviewDetailed 实例对象
     * @return 实例对象
     */
    public ProjectReviewDetailed update(ProjectReviewDetailed projectReviewDetailed){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<ProjectReviewDetailed> chainWrapper = new LambdaUpdateChainWrapper<ProjectReviewDetailed>(projectReviewDetailedMapper);
        if(StrUtil.isNotBlank(projectReviewDetailed.getName())){
            chainWrapper.eq(ProjectReviewDetailed::getName, projectReviewDetailed.getName());
        }
        if(StrUtil.isNotBlank(projectReviewDetailed.getParentIdPath())){
            chainWrapper.eq(ProjectReviewDetailed::getParentIdPath, projectReviewDetailed.getParentIdPath());
        }
        if(StrUtil.isNotBlank(projectReviewDetailed.getOptionIds())){
            chainWrapper.eq(ProjectReviewDetailed::getOptionIds, projectReviewDetailed.getOptionIds());
        }
        if(StrUtil.isNotBlank(projectReviewDetailed.getRemark())){
            chainWrapper.eq(ProjectReviewDetailed::getRemark, projectReviewDetailed.getRemark());
        }
        //2. 设置主键，并更新
        chainWrapper.set(ProjectReviewDetailed::getId, projectReviewDetailed.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(projectReviewDetailed.getId());
        }else{
            return projectReviewDetailed;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Long id){
        int total = projectReviewDetailedMapper.deleteById(id);
        return total > 0;
    }
}
